

<link rel="stylesheet" href="/css/welcome.css" />
<link rel="stylesheet" href="/css/percircle.css" />

<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main welcome">
        <div class="layui-row layui-col-space15">
            <div class="layui-row">
                <div class="layui-col-md12">
                    <div class="layui-row layui-col-space15">
                        <div class="layui-col-md4" id="app">
                            <div class="layui-card">
                                <div class="layui-card-header"><i class="fa fa-file-text icon"></i>任务总览</div>
                                <div class="layui-card-body">
                                    <div class="welcome-module">
                                        <div class="layui-row layui-col-space10">
                                            <div class="layui-col-xs6">
                                                <div class="panel layui-bg-number">
                                                    <div class="panel-body">
                                                        <div class="panel-title">
                                                            <span class="label pull-right layui-bg-blue">实时</span>
                                                            <h5 layui-bg-blue>总任数目</h5>
                                                        </div>
                                                        <div class="panel-content">
                                                            <h1 class="no-margins" style="color:blue">{{job.total}}</h1>
                                                            <small>当天的任务</small>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="layui-col-xs6">
                                                <div class="panel layui-bg-number">
                                                    <div class="panel-body">
                                                        <div class="panel-title">
                                                            <span class="label pull-right layui-bg-green">实时</span>
                                                            <h5>等待的任务</h5>
                                                        </div>
                                                        <div class="panel-content">
                                                            <h1 class="no-margins" style="color:green">{{job.pending}}</h1>
                                                            <small>还未分配</small>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="layui-col-xs6">
                                                <div class="panel layui-bg-number">
                                                    <div class="panel-body">
                                                        <div class="panel-title">
                                                            <span class="label pull-right layui-bg-orange">实时</span>
                                                            <h5>已完成任务</h5>
                                                        </div>
                                                        <div class="panel-content">
                                                            <h1 class="no-margins" style="color:orange">{{job.complete}}</h1>
                                                            <small>正常完成的</small>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="layui-col-xs6">
                                                <div class="panel layui-bg-number">
                                                    <div class="panel-body">
                                                        <div class="panel-title">
                                                            <span class="label pull-right layui-bg-red">实时</span>
                                                            <h5>异常任务</h5>
                                                        </div>
                                                        <div class="panel-content">
                                                            <h1 class="no-margins" style="color:red">{{job.cancel}}</h1>
                                                            <small>取消、打断等</small>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md8">
                            <div class="layui-card">
                                <div class="layui-card-header"><i class="fa fa-life-ring icon"></i>AGV实时信息</div>
                                <div class="layui-card-body" style="padding:0px">
                                    <table class="layui-hide" id="robotTableId" lay-filter="robotTableId"></table>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>

            <div class="layui-row">

                <div class="layui-col-md4" style="padding-right:7.5px">

                    <div class="layui-card">
                        <div class="layui-card-header"><i class="fa fa-tags icon"></i>AGV信息统计</div>
                        <div class="layui-card-body">
                            <div class="welcome-module" id="robotPie" style="min-height:260px">

                            </div>
                        </div>
                    </div>



                </div>
                <div class="layui-col-md8">
                    <div class="layui-card">
                        <div class="layui-card-header"><i class="fa fa-line-chart icon"></i>实时任务</div>
                        <div class="layui-card-body" style="padding:0px">
                            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<script type="text/html" id="stateBar0">
    <div>
        {{# if(d.status=== 0){ }}
        未执行
        {{# }else if(d.status=== 1){ }}
        前往接料
        {{# }else if(d.status=== 2){ }}
        接料中
        {{# }else if(d.status=== 3){ }}
        接料完成
        {{# }else if(d.status=== 4){ }}
        前往送料
        {{# }else if(d.status=== 5){ }}
        送料中
        {{# }else if(d.status=== 6){ }}
        执行中
        {{# }else if(d.status=== 7){ }}
        已完成
        {{# }else if(d.status=== 8){ }}
        被打断
        {{# }else if(d.status=== 9){ }}
        被取消
        {{# }else if(d.status=== 10){ }}
        充电中
        {{# }else{ }}
        未知
        {{# } }}
    </div>

</script>
<script type="text/html" id="tasktype">
    <div>
        {{# if(d.type=== 1){ }}
        接料任务
        {{# }else if(d.type=== 2){ }}
        送料任务
        {{# }else if(d.type=== 3){ }}
        复合任务
        {{# }else if(d.type=== 4){ }}
        充电任务
        {{# }else{ }}
        未知
        {{# } }}
    </div>
</script>
<script type="text/html" id="beginTimeStatus">
    <div>
        {{#  if(d.beginTime !=null){ }}
        <div>{{layui.util.toDateString(d.beginTime, 'HH:mm:ss')}}</div>
        {{#  } else {}}
        <div>{{}}</div>
        {{#  } }}

    </div>

</script>

<script type="text/html" id="updateTimeStatus">
    <div>
        {{#  if(d.updateTime !=null){ }}
        <div>{{layui.util.toDateString(d.updateTime, 'HH:mm:ss')}}</div>
        {{#  } else {}}
        <div>{{}}</div>
        {{#  } }}

    </div>

</script>


<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="cancel">取消</a>
</script>
<script src="../js/signalr/dist/browser/signalr.min.js"></script>
<script src="../js/vue.min.js"></script>

<script>
    layui.use(['form', 'table', 'element', 'echarts','tablePlug'], function () {

        var $ = layui.jquery,
            echarts = layui.echarts,
            table = layui.table;
        var tablePlug = layui.tablePlug;
        tablePlug.smartReload.enable(true);//处理不闪动的关键代码

        var robotCount = 0;

        //渲染任务表格
        var tableIns = table.render({
            elem: '#currentTableId',
            url: 'api/job/GetRealTask',
            smartReloadModel: true,
            parseData: function (res) { //res 即为原始返回的数据

                return {

                    "code": res.code === 200 ? 0 : res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.result, //解析数据长度
                    "data": res.data //解析数据列表
                }
            },
            cols: [[
                { field: 'id', width: 110, title: '任务编号', sort: true, align: "center" },
                { field: 'robotId', width: 100, title: '分配机器人', align: "center" },
                { field: 'type', width: 120, title: '任务类型', align: "center", templet: "#tasktype" },
                { field: 'targets', width: 150, title: '目标点', align: "center" },
                { field: 'status', width: 90, title: '任务状态', align: "center", templet: "#stateBar0" },
                { field: 'currentTarget', width: 90, title: '当前目标', align: "center"},
                { field: 'createTime', width: 120, title: '创建时间', sort: true, align: "center", templet: "<div>{{layui.util.toDateString(d.createTime, 'HH:mm:ss')}}</div>" },
                { field: 'beginTime', width: 120, title: '开始时间', sort: true, align: "center", templet: "#beginTimeStatus" },
                { field: 'updateTime', width: 120, title: '更新时间', sort: true, align: "center", templet: "#updateTimeStatus" },
                { title: '操作', minWidth: 120, toolbar: '#currentTableBar', align: "center" },
                { field: 'remark', width: 150, title: '备注信息', align: "center" },

            ]],
            limit:4,
            limits: ['4', '8', '12'],
            page: true,
            id: 'realtable',
            even: "true",
            loading: false,
            skin: 'row',


        });


             window.time = setInterval(function () {

                tableIns.reload('realtable', { page: { curr: 1 } });

            }, 5000);
        //取消
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'cancel') {
                layer.confirm('您确定要取消任务吗?', function (index) {
                    var id = obj.data.id;
                    $.ajax({
                        method: 'post',
                        contentType: 'application/json;charset=UTF-8',
                        cache: false,
                        url: 'api/job/Abort/' + id,
                        success: function (res) {
                            if (res.code === 200) {
                                console.log(id);

                                layer.msg('取消成功!', { icon: 1 });
                                tablereload("");
                            } else
                                layer.msg(res.msg, { icon: 2 });
                        },
                        error: function (data) {
                            layer.msg('取消出错!', { icon: 2 });

                        }
                    });
                    layer.close(index);
                });
            }
        });


        //渲染机器人涨表格
        var tableRobot = table.render({
            elem: '#robotTableId',
            url: 'api/Robot/GetRealRobotInfo',
            smartReloadModel: true,
            parseData: function (res) { //res 即为原始返回的数据
                robotCount = res.result / 3;

                return {

                    "code": res.code === 200 ? 0 : res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.result, //解析数据长度
                    "data": res.data //解析数据列表
                }
            },
            cols: [[
                { field: 'id', width: 110, title: 'AGV编号', sort: true, align: "center" },
                { field: 'ip', width: 115, title: 'IP', sort: true,align: "center" },
                { field: 'status', width: 120, title: '状态', align: "center" },
                { field: 'mode', width: 120, title: '模式', align: "center" },
                { field: 'bettery', width:100, title: '电量', align: "center",  },
                { field: 'isAvailable', width: 100, title: '是否可用', align: "center",},
                { field: 'isConnected', width: 100, title: '是否连接',  align: "center", },
            ]],
            limits: ["3", "6", "9", "12", "15"],
            limit: 3,
            id: 'realRobotTable',
            page: true,
            even: "true",
            skin: 'row',

        });


        var robotIndex = 0;


        window.time2 = setInterval(function () {

                robotIndex++;
                if (robotIndex > robotCount)
                    robotIndex = 0;
                console.log(robotIndex);
                tableRobot.reload('realRobotTable', { page: { curr: robotIndex } });

            }, 5000);




        //Vue
        var vm = new Vue({
            el: "#app",

            data: {

                job: {
                    total: 0,
                    cancel: 0,
                    complete: 0,
                    pending: 0
                },
            },
            computed: {
                charge: function () {
                    return this.robots.reduce(function (total, current) {
                        current.status.toLowerCase().indexOf("charg") >= 0 ? total++ : total;
                        return total;
                    }, 0);

                },
                offline: function () {

                    return this.robots.reduce(function (total, current) {
                        (!current.isConnected || current.status.toLowerCase().indexOf("stop") >= 0) ? total++ : total;
                        return total;
                    }, 0);

                },
                available: function () {
                    return this.robots.reduce(function (total, current) {
                        current.isAvailable ? total++ : total;
                        return total;
                    }, 0);

                },
                used: function () {
                    return this.robots.reduce(function (total, current) {
                        !(current.currentJob) ? total : total++;
                        return total;
                    }, 0);

                }
            }

        })


        //SignalR
        var connection = new signalR.HubConnectionBuilder().withUrl("/reporthub").build();
        //RobotInfo
        connection.on("RobotInfo", function (robots) {
            vm.robots = robots;
            RobotChart.setOption({
                series: [{
                    type: 'pie', //饼状
                    radius: '65%', //圆的大小
                    center: ['50%', '50%'], //居中
                    label: {
                        normal: {
                            show: true,
                            formatter: '{b}: {c}'
                        }
                    },
                    data: [
                        {
                            value: vm.available,
                            name: '空闲',
                            itemStyle: {
                                color: '#1d953f'

                            }
                        },

                        {
                            value: vm.offline,
                            name: '异常',
                            itemStyle: { color: '#d71345' }
                        },

                        {
                            value: vm.used,
                            name: '占用',
                            itemStyle: {
                                color: '#fcaf17'

                            }
                        },

                        {
                            value: vm.charge,
                            name: '充电',
                            itemStyle: {
                                color: '#009ad6'

                            }
                        },
                    ]
                }]
            });
        });



        //TaskInfo
        //机器人图表
        var RobotPie = {
            title: {
                text: '机器人状态统计',
                x: 'center' //标题居中
            },

            legend: {
                orient: 'vertical', //类型垂直,默认水平
                left: 'left', //类型区分在左 默认居中
                data: ['异常', '占用', '空闲', '充电']
            },
            series: [{
                type: 'pie', //饼状
                radius: '65%', //圆的大小
                center: ['50%', '50%'], //居中
                data: [{
                    value: 4,
                    name: '空闲',
                },
                {
                    value: 1,
                    name: '掉线',
                    itemStyle: { color: '#5fb878' }
                },
                {
                    value: 2,
                    name: '占用',

                },
                {
                    value: 1,
                    name: '充电',

                },
                ]
            }]
        };
        var RobotChart = echarts.init(document.getElementById('robotPie'));
        RobotChart.setOption(RobotPie, true);
        connection.on("TaskInfo", function (jobs) {
            vm.job.total = jobs.total;
            vm.job.cancel = jobs.cancel;
            vm.job.complete = jobs.complete;
            vm.job.pending = jobs.pending;


        });

        //处理链接关闭情况，onclose监听服务器断开和客户端主动断开  ，try catch 监听服务器无法访问等
        //在实际应用中，重新连接超过指定次数后放弃
        async function start() {
            try {
                await connection.start();
                console.log("connected");
            } catch (err) {
                console.log(err);
                setTimeout(() => start(), 5000);
            }
        };
        start();
        connection.onclose(async () => {
            console.info('监听到链接关闭');
            await start();
        });

    });



</script>
